package com.itheima.reggie.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //分页查询name like
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //2.设置分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //执行条件的分页查询
        page = setmealMapper.selectPage(page, wrapper);
        //遍历获取某个套餐
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtils.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据setmeal中的category_id 从category 表查询name
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //6.根据setmeal的id从setmeal_dish 表中查询列表
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;

    }

    @Override
    public void save(Setmeal setmeal) {
        //新增套餐
        //把对象信息分开保存
        //1.保存基本信息
        setmealMapper.insert(setmeal);
        //2.遍历setmeal中的setmealDishs，保存到中间表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());//关联上套餐id
                setmealDishMapper.insert(setmealDish);//执行保存
            }
        }
    }

    @Override
    public void deleteById(Long[] ids) {
           //判断删除的套餐是否还有在售，如果有直接报错
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count>0){
            throw new CustomException("您要删除的套餐有在售，不能删除");

        }
        //到这里是可以删除的
        //先删除套餐表中的所有符合条件的数据
        setmealMapper.deleteBatchIds(Arrays.asList(ids));
        //删除套餐百品中间表中所有符合条件的数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);

    }

    @Override
    public void updateByStatus(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);

        }


    }

    @Override
    public Setmeal findById(Long id) {
        //基本信息的查询
        Setmeal setmeal = setmealMapper.selectById(id);
        //回显菜品
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
       return setmeal;


    }

    @Override
    public void updateById(Setmeal setmeal) {
        //把对象信息分开保存
        //1.保存基本信息
        setmealMapper.updateById(setmeal);
        //2.遍历setmeal中的setmealDishs，保存到中间表
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper1);
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());//关联上套餐id
                setmealDishMapper.insert(setmealDish);//执行保存
            }
        }

    }

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //1. 设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);//category_id=?
        wrapper.eq(Setmeal::getStatus, status);//status=?

        //2. 执行查询
        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<Dish> getmealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;
    }
}
